package com.example.paper.mvp.view.news;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.mvp.model.BaseEntity;
import com.example.mvp.view.BaseVpFragment;
import com.example.paper.R;
import com.example.paper.adapter.ContentNewsAdapter;
import com.example.paper.adapter.ImageBannerAdapter;
import com.example.paper.brean.HttpCode;
import com.example.paper.brean.ImageBannerBean;
import com.example.paper.brean.NewsBean;
import com.example.paper.mvp.contract.LREContract;
import com.example.paper.mvp.model.LREModel;
import com.example.paper.mvp.presenter.LREPresenter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContentNewsFragment extends BaseVpFragment<LREPresenter> implements LREContract.ILREView {

    private long channel_id;
    private SmartRefreshLayout content_news_smart;
    private RecyclerView content_news_rv;
    private View headView;
    private ViewPager headVp;
    private ImageBannerAdapter vpAdapter;
    private ContentNewsAdapter adapter;

    public ContentNewsFragment(long channel_id){
        this.channel_id = channel_id;
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_content_news;
    }

    @Override
    public void initView() {
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_news_banner,null);
        headVp = headView.findViewById(R.id.news_banner_vp);
        content_news_smart = findViewById(R.id.content_news_smart);
        content_news_rv = findViewById(R.id.content_news_rv);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        content_news_rv.setLayoutManager(manager);
        adapter = new ContentNewsAdapter();
        adapter.addHeaderView(headView);
        content_news_rv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        mPresenter = new LREPresenter(new LREModel(),this);
        //根据创建当前fragment对象时的channel_id获取对应新闻数据
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        Map<String,Object>bannerMap = new HashMap<>();
        bannerMap.put("code",HttpCode.IMAGERBANNERCODE);
        mPresenter.requestAll(bannerMap,map);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof ImageBannerBean){
            //banner列表返回
            ImageBannerBean bannerEntity = (ImageBannerBean) entity;
            vpAdapter = new ImageBannerAdapter(bannerEntity.getValues());
            headVp.setAdapter(vpAdapter);
        }
        if (entity instanceof NewsBean){
            //新闻列表返回
            NewsBean newsEntity = (NewsBean) entity;
            adapter.setNewData(newsEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity entity) {
        NewsBean newsEntity = (NewsBean) entity;
        adapter.setNewData(newsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        NewsBean newsEntity = (NewsBean) entity;
        List<NewsBean.Values> list = adapter.getData();
        list.addAll(newsEntity.getValues());
        adapter.setNewData(newsEntity.getValues());
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }
}
